/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BuildTrainSet.Expand;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author Administrator
 */
public final class PrepareExpand {

    private HashMap expnHm = new HashMap();

    public static void main(String argv[]) throws FileNotFoundException, XMLStreamException, SQLException, ClassNotFoundException, UnsupportedEncodingException, IOException {
        PrepareExpand pe = new PrepareExpand("trainset\\hand\\alpha_hand.xml");
    }

    public PrepareExpand(String inf) throws FileNotFoundException, XMLStreamException, SQLException, ClassNotFoundException, UnsupportedEncodingException, IOException {

        System.out.println("package BuildTrainSet.Expand; class PrepareExpand");

        colect(inf);
        writeToFile();
    }

    public void colect(String inF) throws FileNotFoundException, XMLStreamException {
        FileInputStream fis = new FileInputStream(inF);
        XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(fis);

        while (xmlStreamReader.hasNext()) {
            int eventCode = xmlStreamReader.next();
            switch (eventCode) {
                case XMLStreamConstants.START_ELEMENT:
                    String localName = xmlStreamReader.getLocalName();
                    if (localName.equals("W")) {
                        String atValue = xmlStreamReader.getAttributeValue(0);
                        if (atValue.equals("EXPN")) {
                            String expn = xmlStreamReader.getElementText();
                            if (expnHm.containsKey(expn)) {
                                int count = (Integer) expnHm.get(expn);
                                expnHm.put(expn, count + 1);
                            } else {
                                expnHm.put(expn, 1);
                            }
                        }
                    }
            }
        }
    }

    public void writeToFile() throws SQLException, ClassNotFoundException, FileNotFoundException, UnsupportedEncodingException, IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("trainset\\expn\\expn.db"), "UTF8"));
        ArrayList expnAr = new ArrayList(expnHm.entrySet());
        Collections.sort(expnAr, new Comparator() {

            public int compare(Object o1, Object o2) {
                Map.Entry e1 = (Map.Entry) o1;
                Map.Entry e2 = (Map.Entry) o2;
                Integer first = (Integer) e1.getValue();
                Integer second = (Integer) e2.getValue();
                return second.compareTo(first);
            }
        });
        for (int i = 0; i < expnAr.size(); i++) {
            Map.Entry e = (Map.Entry) expnAr.get(i);
            String line = e.getKey() + " " + e.getValue() + "\n";
            bw.write(line);
        }

        System.out.println("So expn la: " + expnAr.size());
        bw.close();
    }
}
